import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * 二
 */
public class RedisDemoString {

    public static void test1(Jedis jedis) {
        System.out.println("mset 和 mget 的使用");
        jedis.flushAll();

        jedis.mset("key1", "111", "key2", "222", "key3", "333");
        List<String> list = jedis.mget("key1", "key2", "key100", "key3");
        System.out.println("values: " + list);

    }

    public static void test2(Jedis jedis) {
        System.out.println("getrange 和 setrange 的使用");
        jedis.flushAll();

        jedis.set("key", "abcdefghij");
        String result = jedis.getrange("key", 2, 5);
        System.out.println("result: " + result);

        jedis.setrange("key", 2, "xyz");
        System.out.println("value: " + jedis.get("key"));
    }

    public static void test3(Jedis jedis) {
        System.out.println("append 的使用");
        jedis.flushAll();

        jedis.set("key", "abcdef");
        jedis.append("key", "ghij");

        String value = jedis.get("key");
        System.out.println(value);
    }

    public static void test4(Jedis jedis) {
        System.out.println("incr 和 decr 的使用");
        jedis.flushAll();

        jedis.set("key", "100");

        long result = jedis.incr("key");
        System.out.println("result: " + result);

        String value = jedis.get("key");
        System.out.println("value: " + value);

        result = jedis.decr("key");
        System.out.println("result: " + result);

        value = jedis.get("key");
        System.out.println("value: " + value);
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        try (Jedis jedis = jedisPool.getResource()) {
            test1(jedis);
            System.out.println();
            test2(jedis);
            System.out.println();
            test3(jedis);
            System.out.println();
            test4(jedis);
        }
    }
}
